var page = 1;
var pageOld = 1;
var pageNum = 10;
var pageI = 1;
var pageL = 5;

var systemUserControlFuntion = function () {
    $.ajax({
        url:"usersWeb/findAllUserFormByStatusPage.json",
        type:'GET',
        dataType:"json",
        data:{
            page:page,pageNum:pageNum,status:2,username:$("input[name='usernameSelect']").val()
        },
        success:function(result){
            var tem = "<tr>" +
                "<th width='16%'>编号</th>" +
                "<th width='37%'>手机号</th>" +
                "<th width='37%'>备注</th>" +
                "<th width='10%'>操作</th>" +
                "</tr>";
            $.each( result.userList, function(index, content){
                tem += "<tr>" +
                    "<td><p>"+content.id+"</p></td>" +
                    "<td><p>"+content.username+"</p></td>" +
                    "<td><p>"+content.sign+"</p></td>" +
                    "<td><p><span class='userSpan'>" +
                    "<a href=\"#\" onclick=\"systemBlackSubmitOK("+content.username+");\">还原</a>" +
                    "</span></p></td>" +
                    "</tr>";
            });
            $("#systemUserControlTable").empty();
            $("#systemUserControlTable").append(tem);

            var pageSum = result.pageSum;
            var numSum = result.numSum;
            var temPage ="<a href='#' onclick='pageFunction("+page+","+page+",\"em\","+pageSum+");'><em>&lt;&lt;</em></a>";

            if(pageSum<=5){
                pageL = pageSum;
            }else if(pageSum>5 && pageSum-pageI < 4 || pageSum-page < 4){
                pageI = pageSum-4;
                pageL = pageSum;
            }else {
                if(page >= 5){
                    if(page > pageOld){
                        if(page-pageI == 4){
                            pageI = page;
                            pageL = page+4;
                        }
                    }else if(page < pageOld){
                        if(pageL-page == 4){
                            pageI = page-4;
                            pageL = page;
                        }
                    }
                }else {
                    pageI = 1;
                    pageL = pageI+4;
                }
            }

            for(var i=pageI;i<=pageL;i++){
                if(page == i){
                    temPage += "<a class='cur' href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }else {
                    temPage += "<a href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }
            }

            temPage += "<a href='#' onclick='pageFunction("+page+","+page+",\"ep\","+pageSum+");'><em>&gt;&gt;</em></a>" +
                "<span>共"+numSum+"条/"+pageSum+"页</span>" +
                "<input class='pageTxt' name='pageValue' type='text' value='' />" +
                "<input class='pageBut' type='button' value='GO' onclick='pageFunction(0,"+page+",\"select\");'/>";

            $("#systemUserControlPage").empty();
            $("#systemUserControlPage").append(temPage);

            //动态加class
            $(".page a,.orderTit a").click(function(){
                $(this).addClass("cur").siblings().removeClass("cur");
            });

            var closeDivFunction = function (){
                $(".deskDiv").fadeOut();
                $(".roleDele").fadeOut();
                $(".roleNew").fadeOut();
                $(".hints").fadeOut();
                $(".authority").fadeOut();
                $(".modifyOrder").fadeOut();
                $(".hintsDiv").fadeOut();
                $(".teamDiv").fadeOut();
                $(".refundDiv").fadeOut();
                $(".addNote").fadeOut();
                $(".evaluation").fadeOut();
            }

            //关闭
            $(".cancel").click(function(){
                closeDivFunction();
            });

            //友情提示
            var hintsPValueFucntion = function(v){
                $("#hintsPValue").html(v);
                $(".hints").css({
                    top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            }
        }
    });
}

systemUserControlFuntion();

var pageFunction = function(p,o,t,s){
    if(t == 'select'){
        if(parseInt($("input[name='pageValue']").val()) < 1){
            page = 1;
        }else {
            page = parseInt($("input[name='pageValue']").val());
        }
        pageI = page - 2;
        pageL = page + 2;
    }else if(t == 'em'){
        if(p == 1){
            page = p;
        }else {
            page = p - 1;
        }
        if(p==5 || pageL-p == 4){
            pageI = p-4;
            pageL = p;
        }
    }else if(t == 'ep'){
        if(p == s){
            page = p;
        }else {
            page = p + 1;
        }
        if(p==5 || p-pageI == 4){
            pageI = p;
            pageL = p+4;
        }
    }else {
        page = p ;
    }
    systemUserControlFuntion();
    pageOld = o;
}

//添加黑名单
$(".newEm").click(function(){
    $("input[name='status']").val('2');
    $(".deskDiv").css({
        display:"block",height:$(document).height()
    });
    $(".roleNew,.teamDiv").css({
        top:($(window).height()-$(".roleNew,.teamDiv").height())/2+$(window).scrollTop()+"px",
        display:"block"
    });
});

//黑名单用户还原
var systemBlackSubmitOK = function(n){
    $("input[name='username']").val(n);
    $("input[name='status']").val('1');
    systemBlackSubmit();
}

//提交黑名单
var systemBlackSubmit = function(){
    $.ajax({
        url:"usersWeb/updateUserWeb.json",
        type:'POST',
        dataType:"json",
        data:$("#systemBlack").serialize(),
        success:function(result){
            if(result.isOK == 'OK'){
                closeDivFunction();
                systemUserControlFuntion();
            }else {
                hintsPValueFucntion(result.msg);
            }
        }
    });
}

//友情提示
var hintsPValueFucntion = function(v){
    $("#hintsPValue").html(v);
    $(".hints").css({
        top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
        display:"block"
    });
}

//关闭
$(".cancel").click(function(){
    closeDivFunction();
});
$(".confirmNo").click(function(){
    closeDivFunction();
});

//关闭
var closeDivFunction = function (){
    $(".deskDiv").fadeOut();
    $(".roleDele").fadeOut();
    $(".roleNew").fadeOut();
    $(".hints").fadeOut();
    $(".authority").fadeOut();
    $(".modifyOrder").fadeOut();
    $(".hintsDiv").fadeOut();
    $(".teamDiv").fadeOut();
    $(".refundDiv").fadeOut();
    $(".addNote").fadeOut();
    $(".evaluation").fadeOut();
}